#!/bin/bash
HOME="/home" #下载文件的目录
NGINX_number="1.29.2"
NGINX_version="nginx-$NGINX_number"
NGINX_tar="$NGINX_version.tar.gz"
NGINX_URL="http://mirrors.sohu.com/nginx/$NGINX_tar"
#NGINX_URL="http://nginx.org/en/download/$NGINX_tar"
INSTALL_DIR="/usr/local" #安装文件的目录
#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}
function installNginx() {
    if [ -d '${INSTALL_DIR}/nginx' ]; then
        echo -e "\033[31m已经有安装nginx,请卸载后重试(find / -name nginx)\033[0m"
        exit
    fi
    rpm -q yum &>/dev/null
    if [ $? -ne 0 ]; then
        echo "请先安装yum工具"
        exit
    fi
    rpm -q pcre &>/dev/null
    if [ $? -ne 0 ]; then
        yum install pcre -y
    fi
    rpm -q pcre-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum install pcre-devel -y
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi
    rpm -q tar &>/dev/null
    if [ $? -ne 0 ]; then
        yum install tar -y
    fi
    rpm -q zlib &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make zlib
    fi
    rpm -q zlib-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make zlib-devel
    fi
    rpm -q gcc-c++ &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make gcc-c++
    fi
    rpm -q libtool &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make libtool
    fi
    rpm -q openssl &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make openssl
    fi
    rpm -q openssl-devel &>/dev/null
    if [ $? -ne 0 ]; then
        yum -y install make openssl-devel
    fi

    if [ ! -d "$HOME" ]; then
        mkdir $HOME
    fi

    cd "$HOME"
    #下载镜像并解压Nginx源码包
    if [ ! -f "$HOME/$NGINX_tar" ]; then
        cd "$HOME"
        wget -c $NGINX_URL && tar -zxf $NGINX_tar
    fi
    if [ ! -d "$HOME/$NGINX_version" ]; then
        cd "$HOME"
        tar -zxf $NGINX_tar
    fi
    #进入解压目录，然后sed修改Nginx版本信息为JWS
    cd $HOME/$NGINX_version
    sed -i -e 's/NGINX_number//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h

    #创建用户组
    createUserGroupAndUser www www
    #预编译
    ./configure --user=www --group=www --prefix=${INSTALL_DIR}/nginx --with-http_stub_status_module --with-http_ssl_module
    if [ $? -ne 0 ]; then
        echo "Nginx预编译失败"
        exit
    else
        echo "Nginx预编译成功"
    fi
    make && make install
    if [ $? -ne 0 ]; then
        echo "make或make install失败"
        exit
    else
        echo "make成功"
    fi
    #设置环境变量
    ln -s ${INSTALL_DIR}/nginx/sbin/nginx /usr/bin/
    echo -e "\033[36m检查配置文件是否正确,运行$INSTALL_DIR/nginx/sbin/nginx -t\033[0m"
    echo -e "\033[36m启动nginx:$INSTALL_DIR/nginx/sbin/nginx然后enter\033[0m"
    echo -e "\033[36m停止nginx:pkill -9 nginx\033[0m"
}

if [[ "$1" = "nginx" ]]; then
    installNginx
fi
